﻿using Newtonsoft.Json;
using Student.ViewModel.Enum;

namespace Student.ViewModel
{
    public class Result : IResult
    {
        /// <summary>
        /// 结果代码（0为成功）
        /// </summary>
        [JsonProperty("code")]
        public int Code { get; set; }

        /// <summary>
        /// 结果消息（成功时为成功消息）
        /// </summary>
        [JsonProperty("msg")]
        public string Msg { get; set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="code">结果代码</param>
        /// <param name="msg">结果信息</param>
        public Result(ResultCode code = ResultCode.Success, string msg = "")
        {
            Code = (int)code;
            Msg = string.IsNullOrEmpty(msg) ? code.GetMsg() : msg;
        }
    }

    public class DataResult<T>:IResult
    {
        /// <summary>
        /// 结果代码（0为成功）
        /// </summary>
        [JsonProperty("code")]
        public int Code { get; set; }

        /// <summary>
        /// 结果消息（成功时为成功消息）
        /// </summary>
        [JsonProperty("msg")]
        public string Msg { get; set; }

        /// <summary>
        /// 返回数据
        /// </summary>
        [JsonProperty("data")]
        public T Data { get; set; }

        public DataResult(T t, ResultCode code = ResultCode.Success, string msg = "")
        {
            Code = (int)code;
            Msg = string.IsNullOrEmpty(msg) ? code.GetMsg() : msg;
            Data = t;
        }
    }

    public interface IResult
    {
        int Code { get; set; }

        string Msg { get; set; }
    }
}
